package com.study.mike.common;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.study.mike.common.utils.HAUtils;
import com.study.mike.common.utils.RedisUtil;
import com.study.mike.mapper.UserMapper;
import com.study.mike.model.entity.Book;
import com.study.mike.model.entity.User;
import org.junit.jupiter.api.Test;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Kh
 */
@SpringBootTest
public class RedisTestTwo {
    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisProperties redisProperties;

    @Resource
    private RedisTemplate<String, String> stringRedisTemplate;


    @Test
    void contextLoads() {
//        redisTemplate.opsForValue().set("name","康辉");
//        System.out.println(redisTemplate.opsForValue().get("name"));
//        User user = new User();
//        user.setEmail("12331");
//        user.setName("kh1");
//        redisTemplate.opsForValue().set("user1", user);

//        System.out.println(redisTemplate.opsForValue().get("USER_BUCKET_KEY::kh1468936833336369174"));
    }

    @Test
    void test() {
        redisUtil.set("age", 15);

        System.out.println(redisUtil.get("age"));
    }

    @Test
    void one() {
        System.out.println(redisUtil.get("password"));
        Config config = new Config();
        //设置单个redis服务
        config.useSingleServer().setAddress("redis://118.31.76.49:6379");
        RedissonClient redisson = Redisson.create(config);
        RLock rLock = redisson.getLock("a");
    }

    @Test
        // 这里获取到的数据都是list类型的数据 ！！！！！（cao）
    void getUserInfo() throws Exception {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>();
//        wrapper.eq(User::getId, "1468936833336369233");
//        List<User> user = userMapper.selectList(null);
        redisUtil.lSet("userList", userMapper.selectList(null));
        List<Object> user = redisUtil.lGet("userList", 0, -1);
//        System.out.println(user);
//        for (int i = 0; i < user.size(); i++) {
//            Object[] objects = (Object[]) user.get(i);
//            System.out.println(objects[1]);
//
//        }
//
//        for (Object obj : user) {
//              List user1 = (List) obj;
//            System.out.println(user1.size());
////            System.out.println(user1.get(0).);
//        }


//        for (Object o : user) {
//            Map entity = (Map)o;
//            System.out.println(entity.get("name").toString());
//            System.out.println(o);
//        }
//        System.out.println(user.get(0));

//        List<User> collect = user.stream().collect(Collectors.toList());
//        List<User> userList = HAUtils.objToList(user,User.class);
//        System.out.println(userList);

//        user.forEach(item -> {
//            ObjectMapper objectMapper = new ObjectMapper();
//            User userItem = objectMapper.convertValue(item, User.class);
//            userList.add(userItem);
//        });
//        userList.forEach(item->{
//            System.out.println(item.getName());
//        });

    }

    @Test
    void test5() {
        System.out.println(StrUtil.sub("J00029500000001", 4, -1));
        int length = "J000295000000011".length();
        System.out.println("J000295000000011".substring(4, length));
        Long a = 10000000000000L;
        System.out.println(a + 1);
        System.out.println(a.toString() + "hello");

        System.out.println(StrUtil.removeSuffix("hello", "ello"));
        System.out.println("h".concat("ello"));

    }


    /**
     * 获取多个键的值
     */
    @Test
    void redisTestMget() {
        List<String> list = new ArrayList<>();
        list.add("name");
        list.add("gender");
        list.add("password");
        System.out.println(redisUtil.mget(list));
    }

    /**
     * 获取hash的数据值
     */
    @Test
    void redisGetHashTest() {
//        System.out.println(redisUtil.hget("myhash", "name"));
        System.out.println(redisUtil.hmget("myhash"));
    }

    @Test
    void objectTest() {
        System.out.println(redisUtil.getAllKeys());
    }

    @Test
    void getInfo() {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>();
        wrapper.eq(User::getId, "1468936833336369233");
//        List<User> user = userMapper.selectList(null);
        User user = userMapper.selectOne(wrapper);
        redisUtil.set("user", user);

        System.out.println(redisUtil.get("453C353A0CCFA15EDB6ACA8569945298"));
        System.out.println(redisUtil.get("user"));

        List<User> list = (List<User>) redisUtil.get("user");
//        System.out.println(list);
        list.forEach(item -> {
            System.out.println(item.getName());
        });
    }

    @Test
    void testRedisTel() {
        // 过期时间三个小时
        Book book = new Book();
        book.setBookAuthor("gjr");
        book.setBookName("啊哈");
        String redisKey = "test:info" + book.hashCode();
        stringRedisTemplate.opsForValue().set(redisKey, "test data really");

    }
    @Test
    void testAppend() {
        //
        String redisKey = "test:append:sn";
        redisTemplate.opsForValue().append(redisKey,"two");
    }


}
